package com.ruoyi.parkingcar.service.impl;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ruoyi.parkingcar.domain.ParkRules;
import com.ruoyi.parkingcar.mapper.ParkCarMapper;
import com.ruoyi.parkingcar.mapper.ParkRulesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.parkingcar.mapper.ParkOrderMapper;
import com.ruoyi.parkingcar.domain.ParkOrder;
import com.ruoyi.parkingcar.service.IParkOrderService;

/**
 * 订单信息管理Service业务层处理
 * 
 * @author 饼干
 * @date 2025-02-09
 */
@Service
public class ParkOrderServiceImpl implements IParkOrderService 
{
    @Autowired
    private ParkOrderMapper parkOrderMapper;

    @Autowired
    private ParkRulesMapper parkRulesMapper;



    /**
     * 查询订单信息管理
     * 
     * @param orderId 订单信息管理主键
     * @return 订单信息管理
     */
    @Override
    public ParkOrder selectParkOrderByOrderId(Long orderId)
    {
        return parkOrderMapper.selectParkOrderByOrderId(orderId);
    }

    /**
     * 查询订单信息管理列表
     * 
     * @param parkOrder 订单信息管理
     * @return 订单信息管理
     */
    @Override
    public List<ParkOrder> selectParkOrderList(ParkOrder parkOrder)
    {
        System.out.println(parkOrderMapper.selectParkOrderList(parkOrder) + "434343433---------");
        return parkOrderMapper.selectParkOrderList(parkOrder);
    }

    /**
     * 新增订单信息管理
     * 
     * @param parkOrder 订单信息管理
     * @return 结果
     */
    @Override
    public int insertParkOrder(ParkOrder parkOrder)
    {


        return parkOrderMapper.insertParkOrder(parkOrder);
    }

    /**
     * 修改订单信息管理
     * 
     * @param parkOrder 订单信息管理
     * @return 结果
     */
    @Override
    public int updateParkOrder(ParkOrder parkOrder)
    {
        return parkOrderMapper.updateParkOrder(parkOrder);
    }

    /**
     * 批量删除订单信息管理
     * 
     * @param orderIds 需要删除的订单信息管理主键
     * @return 结果
     */
    @Override
    public int deleteParkOrderByOrderIds(Long[] orderIds)
    {
        return parkOrderMapper.deleteParkOrderByOrderIds(orderIds);
    }

    /**
     * 删除订单信息管理信息
     * 
     * @param orderId 订单信息管理主键
     * @return 结果
     */
    @Override
    public int deleteParkOrderByOrderId(Long orderId)
    {
        return parkOrderMapper.deleteParkOrderByOrderId(orderId);
    }


    //查询车辆进出
    @Override
    public ParkOrder selectCarByNumber(String licensePlate) {

        return parkOrderMapper.selectCarByNumber(licensePlate);
    }

    //根据车辆ID和时间新增
    @Override
    public int insertOrder(ParkOrder parkOrder) {
        parkOrder.setOrderStatus(1);

        return parkOrderMapper.insertOrder(parkOrder);
    }

    /**
     * Vant
     */
    //查询
    @Override
    public List<ParkOrder> select(ParkOrder parkOrder){
        return parkOrderMapper.selectParkOrder(parkOrder);
    }

    //查询
    @Override
    public List<ParkOrder> selects(ParkOrder parkOrder){
        return parkOrderMapper.selectOrder(parkOrder);
    }

    //删除
    @Override
    public int delete(ParkOrder parkOrder) {return parkOrderMapper.deleteByPrimaryKey(parkOrder.getOrderNo());}

    //修改
    @Override
    public int updateOrder(ParkOrder parkOrder) {
        return parkOrderMapper.updateOrder(parkOrder);
    }

    @Override
    public void updateOrderById(ParkOrder parkOrder) {
         parkOrderMapper.updateOrderById(parkOrder);
    }

    ////添加出场时间
    @Override
    public void addEndTime(ParkOrder parkOrder) {


    }

    //订单结算
    @Override
    public BigDecimal constOrder(ParkOrder parkOrder) {


//        long carId = parkOrder.getCarId();
        String orderNo = parkOrder.getOrderNo();
        Date endTime = parkOrder.getEndTime();
        Date startTime = parkOrder.getStartTime();
        //计算
        BigDecimal cost = getcost(endTime,startTime);

        parkOrderMapper.addCost(cost,orderNo,endTime);
        return cost;
    }

    //计算停车费用
    public BigDecimal getcost(Date endTime,Date startTime) {

        //查询规则
        ParkRules parkRules = parkRulesMapper.selectByOrder();

        //获取时间金额字段
        Long freeTime = parkRules.getFreeTime();
        BigDecimal free0To2 =parkRules.getBasePrice();
        BigDecimal free2 = parkRules.getPrice();
        BigDecimal max = parkRules.getMaxPrice();
        //初始金额
        BigDecimal minFree = new BigDecimal("0");
        //计算时长
        long time = endTime.getTime() - startTime.getTime();
        long hour = time / (60 * 60 * 1000);
        long day = time / (1000 * 60 * 60 * 24);

        if(hour <= freeTime){
            BigDecimal fee = new BigDecimal("0");
            return fee;  //返回免费时间
        }

        //超过24小时部分
        long remainHour = hour ;
        for(long i = 0 ; i <= day ; i++){
            //当天停车时间
            long nowDayHours = getnowDayHours(endTime, startTime, i);
            //当天停车费用
            BigDecimal nowFee =costDayFee(nowDayHours, freeTime, free0To2, free2, max);
            minFree = minFree.add(nowFee);
            //减
            remainHour = remainHour - nowDayHours;
        }
        return minFree;

    }

    public BigDecimal costDayFee(long hour, Long freeTime, BigDecimal fee0To2h, BigDecimal feeOver2h, BigDecimal maxFee) {
        //总费用--初始为0
        BigDecimal dayFee = new BigDecimal("0");
        //计算免费费用
        if (hour <= freeTime){
            BigDecimal fee = new BigDecimal("0");
            return fee;//免费时间
        }
        //计算0-2小时费用
        if (hour <= 2){
            dayFee = fee0To2h.multiply(new BigDecimal(hour - 1));//减去免费时间
        }else {
            // 如果停车时间大于2小时，先计算0-2小时的费用
            dayFee = fee0To2h.multiply(new BigDecimal(1)); // 1小时免费+1小时按收费标准计算
            // 计算超过2小时的费用
            long over2Hours = hour - 2;
            dayFee = dayFee.add(feeOver2h.multiply(new BigDecimal(over2Hours)));
        }
        // 单日封顶，超过100元按100元计
        if (dayFee.compareTo(maxFee) > 0) {
            dayFee = maxFee;
        }
        return dayFee;
    }


    public long getnowDayHours(Date endTime,Date startTime,long dayOffset) {

        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(startTime);
        startCalendar.add(Calendar.DAY_OF_YEAR, (int) dayOffset);

        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(endTime);
        endCalendar.add(Calendar.DAY_OF_YEAR, (int) dayOffset);

        // 如果是最后一天，停车时长为结束时间到当天的23:59:59
        if (dayOffset == 0) {
            return (endCalendar.getTimeInMillis() - startCalendar.getTimeInMillis()) / (1000 * 60 * 60);
        } else if (startCalendar.getTimeInMillis() < endCalendar.getTimeInMillis()) {
            // 判断当天停车时间是满24小时还是部分时段
            long currentDayEndMillis = endCalendar.getTimeInMillis();
            long currentDayStartMillis = startCalendar.getTimeInMillis();
            return (currentDayEndMillis - currentDayStartMillis) / (1000 * 60 * 60); // 小时
        }
        return 0;
    }





}
